package com.sales.controller;

import com.sales.entity.ResCode;
import com.sales.excption.SalesException;
import com.sales.service.CommissionDetailsService;
import com.sales.service.ProfileService;
import com.sales.signUpReq.ReferralCodeSignUpReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by lihu on 2017/9/12.
 */
@Slf4j
@RestController
@RequestMapping(value = "/api/v1/sales", produces = "application/json;charset=utf-8")
public class CheckCodeController {

    @Autowired
    ProfileService profileService;

    @Autowired
    CommissionDetailsService commissionDetailsService;

    /**
     * 校验邀请码
     *
     * @param referralCode
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/check/referral-code")
    public ResponseEntity checkeReferralCode(@RequestParam("referralCode") String referralCode) {
        try {
            profileService.checkCode(referralCode);
            return new ResponseEntity(HttpStatus.OK);
        } catch (SalesException ex) {
            log.error("checkeReferralCode is error=>referralCode:{}", referralCode, ex);
            return new ResponseEntity(ex.getMessage(), HttpStatus.BAD_REQUEST);
        } catch (Exception ex) {
            log.error("checkeReferralCode is error=>referralCode:{}", referralCode, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 减少推荐码的次数
     * @param refCode
     * @return
     */
    @GetMapping(value = "check/reduce/refCode")
    public ResponseEntity reduceRefCode(@RequestParam("refCode") String refCode){
        try {
            profileService.reduceRefCode(refCode);
            return new ResponseEntity(HttpStatus.OK);
        } catch (SalesException ex) {
            log.error("count is not enough=>referralCode:{}", refCode, ex);
            return new ResponseEntity(ex.getMessage(), HttpStatus.BAD_REQUEST);
        } catch (Exception ex) {
            log.error("count is not enough=>referralCode:{}", refCode, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }


    }




    /**
     * 邀请码 信息同步
     *
     * @param referralCode
     * @param
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/check/referral-code")
    public ResponseEntity referralCode(@Valid @RequestBody ReferralCodeSignUpReq referralCode,
                                       BindingResult bindingResult) {
        try {

            //参数校验
            if (bindingResult.hasErrors()) {
                log.info("【邀请码信息同步失败】参数不正确, referralCode={}", referralCode);
                return new ResponseEntity(bindingResult.getFieldError().getDefaultMessage(),
                        HttpStatus.BAD_REQUEST);
            }
            commissionDetailsService.initCode(referralCode);
            return new ResponseEntity(HttpStatus.OK);
        } catch (SalesException ex) {
            log.error("referralCode is error=>referralCode:{}", referralCode, ex);
            return new ResponseEntity(ex.getMessage(), HttpStatus.BAD_REQUEST);
        } catch (Exception ex) {
            log.error("referralCode is error=>referralCode:{}", referralCode, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }
}
